﻿using HomagGroup.Base.UI.Themes.Resources;
using HomagGroup.Base.UI.Windows;
using HomagGroup.Base.UI;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using HomagChina.CCT.DataTypeModel;

namespace HomagChina.CCT
{
    public class Utility
    {
        public static object GetViewModelInstance(string typeName)
        {
            typeName = "HomagChina.CCT.ViewModels." + typeName.Split('.')[3] + "ViewModel";
            var viewModelType = Type.GetType(typeName);
            if (viewModelType != null)
            {
                var viewModelInstance = Activator.CreateInstance(viewModelType);
                return viewModelInstance;
            }
            else
            {
                return null;
            }
        }

        public static UserControl GetUserControlInstance(string typeName)
        {
            typeName = "HomagChina.CCT.Views." + typeName;
            var userControlType = Type.GetType(typeName);
            if (userControlType != null)
            {
                var userControlInstance = (UserControl)Activator.CreateInstance(userControlType);
                return userControlInstance;
            }
            else
            {
                return null;
            }
        }

        public static bool CloseDiaLog(string Title)
        {
            bool result = false;
            try
            {
                foreach (System.Windows.Window window in Application.Current.Windows)
                {
                    if (window.Title.Equals(Title))
                    {
                        window.Close();
                        return result = true;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        public void OpenInformationCommand(InformationModel Data)
        {
            var version = new Version(Data.Version == null ? "" : Data.Version);
            var Desc = Data.Describe;
            var Title = Data.Title;
            var licenses = new List<string>(Data.licenses.Split(','));
            var appIcon = Application.Current.TryFindResource(InternalKeys.MessageInformationImage) as DrawingBrush;
            var components = new Dictionary<string, string>
            {
                { "powerTouch", Data.powerTouch},
                { "powerControl", Data.powerControl},
                { "HomagGroup.Base.Ui", Data.HomagGroupBaseUi },
                { "Equipment Number",Data.EquipmentNumber},
                { "Main Operating Interface", Data.MainOperatingInterface },
                { "Server Address", Data.ServerAddress },
                { "DataBase Server" ,Data.DataBaseServer },
                { "Database Name", Data.DataBaseName}
            };
            var dialog = new InfoDialog(Desc, Title, version, 1, appIcon, components, licenses);
            dialog.SetValue(UIAdjustments.DisplayModeProperty, DisplayMode.NonTouch);
            dialog.Owner = UIAdjustments.ActiveWindow;
            dialog.ShowDialog();
        }

        //调用基类的GetLocaText方法
        public static string GetLocaText(string text)
        {
            return HomagGroup.BaseUI.Properties.Resources.ResourceManager.GetString(text, UIAdjustments.CurrentThreadLanguageManager.CurrentCulture);
        }
    }
}